package cn.colat.micse.common.utils;

import cn.colat.micse.common.code.ResCode;
import cn.colat.micse.common.exception.GlobalServerException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

public class AssertUtil {

    /**
     * 对象是否为空 只判断对象是否==null
     *
     * @param obj     对象
     * @param message 错误消息
     */
    public static void objIsNull(Object obj, String message) {
        if (ObjectUtils.isEmpty(obj)) {
            throw new GlobalServerException(message);
        }
    }

    /**
     * 对象是否为空 只判断对象是否==null
     *
     * @param obj     对象
     * @param resCode 响应状态码
     */
    public static void objIsNull(Object obj, ResCode resCode) {
        if (ObjectUtils.isEmpty(obj)) {
            throw new GlobalServerException(resCode);
        }
    }

    public static void objIsNull(Object obj, ResCode resCode, Object... arg) {
        if (ObjectUtils.isEmpty(obj)) {
            ResCode code = resCode.format(arg);
            throw new GlobalServerException(code);
        }
    }


    public static void strIsEmpty(String str, String message) {
        if (!StringUtils.hasLength(str)) {
            throw new GlobalServerException(message);
        }
    }

    public static void strIsEmpty(String str, ResCode resCode) {
        if (!StringUtils.hasLength(str)) {
            throw new GlobalServerException(resCode);
        }
    }

    public static void strIsEmpty(String str, ResCode resCode, Object... arg) {
        if (!StringUtils.hasLength(str)) {
            ResCode code = resCode.format(arg);
            throw new GlobalServerException(code);
        }
    }


    public static void listIsEmpty(Collection<?> list, String message) {
        if (CollectionUtils.isEmpty(list)) {
            throw new GlobalServerException(message);
        }
    }

    public static void listIsEmpty(Collection<?> list, ResCode resCode) {
        if (CollectionUtils.isEmpty(list)) {
            throw new GlobalServerException(resCode);
        }
    }

    public static void listIsEmpty(Collection<?> list, ResCode resCode, Object... arg) {
        if (CollectionUtils.isEmpty(list)) {
            ResCode code = resCode.format(arg);
            throw new GlobalServerException(code);
        }
    }

    public static void isMapEmpty(Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new GlobalServerException(message);
        }
    }

    public static void isMapEmpty(Map<?, ?> map, ResCode resCode) {
        if (CollectionUtils.isEmpty(map)) {
            throw new GlobalServerException(resCode);
        }
    }

    public static void isMapEmpty(Map<?, ?> map, ResCode resCode, Object... arg) {
        if (CollectionUtils.isEmpty(map)) {
            ResCode code = resCode.format(arg);
            throw new GlobalServerException(code);
        }
    }

    /**
     * 成立报错
     */
    public static void isBoolean(boolean condition, String message) {
        if (condition) {
            throw new GlobalServerException(message);
        }
    }

    /**
     * 成立报错
     */
    public static void isBoolean(boolean condition, ResCode resCode) {
        if (condition) {
            throw new GlobalServerException(resCode);
        }
    }

    /**
     * 成立报错
     */
    public static void isBoolean(boolean condition, ResCode resCode, Object... arg) {
        if (condition) {
            ResCode code = resCode.format(arg);
            throw new GlobalServerException(code);
        }
    }

    /**
     * 不成立报错
     */
    public static void checkBoolean(boolean condition, String message) {
        if (!condition) {
            throw new GlobalServerException(message);
        }
    }

    /**
     * 不成立报错
     */
    public static void checkBoolean(boolean condition, ResCode resCode) {
        if (!condition) {
            throw new GlobalServerException(resCode);
        }
    }

    /**
     * 不成立报错
     */
    public static void checkBoolean(boolean condition, ResCode resCode, Object... arg) {
        if (!condition) {
            ResCode code = resCode.format(arg);
            throw new GlobalServerException(code);
        }
    }
}
